package com.db.junit.state;

import com.db.junit.IAccount;
import com.db.junit.LoginService;
import com.db.junit.exception.AccountRevokedException;

/**
 * User: Yury
 */
public abstract class LoginServiceState {
    public void login(IAccount account, String password, LoginService context) throws AccountRevokedException {
        if (account.passwordMatches(password)) {
            if (account.isRevoked()) {
                throw new AccountRevokedException("Account is already revoked");
            }
            account.setLoggedIn(true);
            context.setState(account.getUserName(), new FirstFailedAttempt());
        } else {
            dealWithFailedAttempt(account, context);
        }
    }

    protected abstract void dealWithFailedAttempt(IAccount account, LoginService context);
}
